package com.chencq.module.user.controller;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chencq.core.entity.Pager;
import com.chencq.module.user.entity.User;
import com.chencq.module.user.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	protected Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	UserService userService;

	@RequestMapping("/list")
	public String toList(HttpServletRequest request, HttpServletResponse response,User user,Pager page, ModelMap model) {
		Pager pager = userService.findPageList(user,page.getPageNo(), page.getPageSize());
		model.put("pager", pager);
		return "user/list";
	}
	
	@RequestMapping("/get/{userId}")
	@ResponseBody
	public String getById(@PathVariable String userId, HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		User user = (User) userService.getById(userId);
		return user.toString();
	}
	
	@RequestMapping("/delete/{userId}")
	@ResponseBody
	public String delete(@PathVariable String userId, HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		boolean status = false;
		String msg = "";
		User user = userService.getById(userId);
		if (user != null) {
			userService.delete(user);
			msg = "删除成功";
			status = true;
		} else {
			msg = "删除失败";
		}
		JSONObject json = new JSONObject();
		json.put("status", status);
		json.put("msg", msg);
		return json.toString();
	}
	@RequestMapping("/addUI")
	public String toAdd(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		return "user/edit";
	}
	@RequestMapping("/add")
	public String add(User user, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		user.setUserId(UUID.randomUUID().toString());
		user.setPassWord("123456");
		user.setStatus(1);
		userService.save(user);
		return "redirect:/user/list";
	}
	
	@RequestMapping("/updateUI/{userId}")
	public String toEdit(@PathVariable String userId, HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		User user = userService.getById(userId);
		model.put("user", user);
		return "user/edit";
	}
	
	@RequestMapping("/update")
	public String update(User user, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		User t=userService.getById(user.getUserId());
		t.setUserName(user.getUserName());
		t.setNickName(user.getNickName());
		t.setGender(user.getGender());
		t.setCellphone(user.getCellphone());
		t.setEmail(user.getEmail());
		t.setHobby(user.getHobby());
		userService.update(t);
		return "redirect:/user/list";
	}
	
	/**
	 * 修改密码界面
	 */
	@RequestMapping("resetPasswordUI")
	public String personalCenter(HttpServletRequest req, HttpServletResponse res, ModelMap model,User user){
		//查询个人信息
		User currentUser = new User();
		currentUser = userService.getById(user.getUserId());
		model.put("user", currentUser);
		return "user/resetPassword";
	}
	
	/**
	 * 修改密码
	 */
	@RequestMapping("password/update")
	@ResponseBody
	public String resetPassword (HttpServletRequest req, HttpServletResponse res, ModelMap model, User user){
		String msg = "修改成功";
		try {
			if(!StringUtils.isBlank(user.getUserId())&&!StringUtils.isBlank(user.getPassWord())){
				//根据id获取用户
				User t=userService.getById(user.getUserId());
				t.setPassWord(user.getPassWord());
				userService.update(t);
			}
		} catch (Exception e) {
			msg = "修改失败";
		}
		return msg;
	}
	
}
